package net.wanho.systemSettings.dao.impl;

import net.wanho.commons.dao.impl.BaseDAOImpl;
import net.wanho.commons.exception.DAOException;
import net.wanho.commons.exception.DataAccessException;
import net.wanho.systemSettings.dao.EmployeeDAO;
import net.wanho.systemSettings.po.Employee;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class EmployeeDAOImpl extends BaseDAOImpl<Employee> implements EmployeeDAO {
    @Override
    public boolean insert(Employee employee) throws DAOException {

        try {
            String sql = "insert\n" +
                    " into\n" +
                    " employee (EMPLOYEE_NAME,\n" +
                    " DEPARTMENT_ID,\n" +
                    " POSITON_ID,\n" +
                    " PHONE,\n" +
                    " EMAIL,\n" +
                    " STATUS,\n" +
                    " PARENT_EMPLOYEE_ID,\n" +
                    " CREATE_TIME,\n" +
                    " UPDATE_TIME)\n" +
                    "values(?,?,?,?,?,?,?,now(),now())";
            Object params [] = {employee.getEmployeeName(),employee.getDepartmentId(),employee.getPositionId(),employee.getPhone(),
            employee.getEmail(),0,employee.getParentEmployeeId()};
            return super.jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean delete(String id) throws DAOException {
        return super.delete(id);
    }

    @Override
    public boolean update(Employee employee) throws DAOException {
        try {
            String sql = "update\n" +
                    " employee\n" +
                    "set\n" +
                    " EMPLOYEE_NAME = ?,\n" +
                    " DEPARTMENT_ID = ?,\n" +
                    " POSITON_ID = ?,\n" +
                    " PHONE = ?,\n" +
                    " EMAIL = ?,\n" +
                    " PARENT_EMPLOYEE_ID = ?,\n" +
                    " CREATE_TIME = now(),\n" +
                    " UPDATE_TIME = now()\n" +
                    " where\n" +
                    " EMPLOYEE_ID = ?\n";
            Object [] params = {employee.getEmployeeName(),employee.getDepartmentId(),employee.getPositionId(),employee.getPhone(),
            employee.getEmail(),employee.getParentEmployeeId(),employee.getEmployeeId()};
            return super.jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public Employee findById(String id) throws DAOException {
        try {
            String sql = "SELECT \n" +
                    "  e.EMPLOYEE_NAME employeeName,\n" +
                    "  e.PHONE phone,\n" +
                    "  e.EMAIL email,\n" +
                    "  e.STATUS \"status\",\n" +
                    "  e.PARENT_EMPLOYEE_ID parentEmployeeId,\n" +
                    "  e.CREATE_TIME createTime,\n" +
                    "  e.UPDATE_TIME updateTime,\n" +
                    "  e.EMPLOYEE_ID employeeId,\n" +
                    "  l.EMM_PASSWORD \"logIn.emmPassword\",\n" +
                    "  e.DEPARTMENT_ID \"department.departmentId\",\n" +
                    "  d.DEPARTMENT_NAME \"department.departmentName\",\n" +
                    "  e.POSITON_ID \"position.positionId\",\n" +
                    "  p.POSITION_NAME \"position.positionName\",\n" +
                    "  p.POSITION_LEVEL \"position.positionLevel\",\n" +
                    "  p.CREATE_TIME \"position.createTime\",\n" +
                    "  p.UPDATE_TIME \"position.updateTime\" \n" +
                    "FROM\n" +
                    "  employee e \n" +
                    "  RIGHT JOIN log_in l \n" +
                    "    ON e.EMPLOYEE_ID = l.EMPLOYEE_ID \n" +
                    "  LEFT JOIN department d \n" +
                    "    ON d.DEPARTMENT_ID = e.DEPARTMENT_ID \n" +
                    "  LEFT JOIN emm_position p \n" +
                    "    ON p.POSITION_ID = e.POSITON_ID \n" +
                    "WHERE e.EMPLOYEE_ID =?" ;
            Object[] params = {id};
            return super.queryForObject(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<Employee> selectAll() throws DAOException {
        try {
            String sql = "select\n" +
                    " e.EMPLOYEE_ID employeeId,\n" +
                    " e.EMPLOYEE_NAME employeeName,\n" +
                    " d.DEPARTMENT_NAME \"department.departmentName\",\n" +
                    " ep.POSITION_NAME \"position.positionName\",\n" +
                    " e.PHONE phone,\n" +
                    " e.EMAIL email,\n" +
                    " e.STATUS status,\n" +
                    " e.PARENT_EMPLOYEE_ID parentEmployeeId,\n" +
                    " e.CREATE_TIME createTime,\n" +
                    " e.UPDATE_TIME updateTime\n" +
                    "from\n" +
                    " employee e\n" +
                    "left join emm_position ep on\n" +
                    " e.POSITON_ID = ep.POSITION_ID\n" +
                    "left join department d on\n" +
                    " e.DEPARTMENT_ID = d.DEPARTMENT_ID";
            Object[] params = {};
            return super.queryForList(sql, params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public List<Employee> selectByCond(Map<String, String> condMap) throws DAOException {

        try {
            String sql = "select\n" +
                    " e.EMPLOYEE_ID employeeId,\n" +
                    " e.EMPLOYEE_NAME employeeName,\n" +
                    " d.DEPARTMENT_NAME \"department.departmentName\",\n" +
                    " ep.POSITION_NAME \"position.positionName\",\n" +
                    " e.PHONE phone,\n" +
                    " e.EMAIL email,\n" +
                    " e.STATUS status,\n" +
                    " e.PARENT_EMPLOYEE_ID parentEmployeeId,\n" +
                    " e.CREATE_TIME createTime,\n" +
                    " e.UPDATE_TIME updateTime\n" +
                    " from\n" +
                    " employee e\n" +
                    "left join emm_position ep on\n" +
                    " e.POSITON_ID = ep.POSITION_ID\n" +
                    "left join department d on\n" +
                    " e.DEPARTMENT_ID = d.DEPARTMENT_ID\n" +
                    "WHERE 1 = 1 ";
            //动态拼接SQL
            StringBuffer sb = new StringBuffer(sql);
            //条件无效  或者 没有条件
            boolean isCond = condMap == null || condMap.isEmpty();
            //创建集合 存放 动态参数值 ？ 替换的值
            List<Object> condParamValueList = new ArrayList<>();
            //有效条件
            if (!isCond) {
                condMap.forEach((k, v) -> {
                    sb.append(" AND ").append(k).append(" LIKE  ?");
                    condParamValueList.add("%" + v + "%");
                });
            }
            Object[] params = condParamValueList.toArray();
            return super.queryForList(sb.toString(), params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage());
        }
    }

    @Override
    public boolean updelete(Employee employee) throws DAOException {
        try {
            String sql = "update\n" +
                    " employee\n" +
                    "set\n" +
                    " STATUS = ?,\n" +
                    "where\n" +
                    " EMPLOYEE_ID = ?";
            Object [] params = {employee.getStatus(),employee.getEmployeeId()};
            return super.jdbcTemplate.update(sql,params);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException();
        }
    }

}

